/**
 * Copyright (c) 2011 Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package wiiWired.controller.mote;

import wiiWired.controller.console.RaceTableModel;
import wiiWired.controller.data.Race;
import wiiWired.controller.data.RaceModel;

/**
 * Controls the timing
 * 
 * @author Daniel Centore
 * 
 */
public class TimerController extends Thread
{

	private static long startTime = System.currentTimeMillis();
	private static boolean isTiming = false;
	private static Race currentRace;

	public static void sendStartSignal(RaceModel raceModel)
	{
		//		MoteFinder.getMoteFinder().stopDiscovery();
		MoteController[] controller = ControllerModel.getModel();

		//		Race r = getCurrentRace();
		//
		//		// register previous race
		//		if (r.raceNumber >= 0)
		//			//			RaceTableModel.registerRace(r);
		//			raceModel.addRace(r);

		raceModel.addRace(currentRace = new Race());

		startTime = System.currentTimeMillis();
		isTiming = true;

		for (MoteController m : controller)
		{
			m.resetTime();
			m.setTiming(true);
		}

		TimerLights.resetLights();
	}

	public static Race getCurrentRace()
	{
		//		MoteController[] controller = ControllerModel.getModel();
		//
		//		long[][] motes = new long[controller.length][];
		//		long[][] nunchuks = new long[controller.length][];
		//
		//		for (int i = 0; i < motes.length; i++)
		//		{
		//			motes[i] = controller[i].getTimes();
		//			nunchuks[i] = controller[i].getNunchukTimes();
		//		}
		//
		//		Race r = new Race(motes, nunchuks, controller);
		//		return r;
		return currentRace;
	}

	public static long getCurrentTime()
	{
		return System.currentTimeMillis() - startTime;
	}

	public static String getCurrentFormattedTime()
	{
		return RaceTableModel.formatTime(getCurrentTime());
	}

	public static boolean isTiming()
	{
		return isTiming;
	}

	public static void setTiming(boolean isTiming)
	{
		TimerController.isTiming = isTiming;
	}

}
